package com.wxg.config.rabbitmq;

import com.wxg.common.core.dto.Address;
import com.wxg.domain.RabbitMqConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Conditional;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Date;

/**
 * @author void
 * @date 2019/12/25 17:06
 * @desc
 */
@Slf4j
@Component
@Conditional(value = RabbitCondition.class)
public class RabbitSender {

    /**
     Broker:它提供一种传输服务,它的角色就是维护一条从生产者到消费者的路线，保证数据能按照指定的方式进行传输,
     Exchange：消息交换机,它指定消息按什么规则,路由到哪个队列。
     Queue:消息的载体,每个消息都会被投到一个或多个队列。
     Binding:绑定，它的作用就是把exchange和queue按照路由规则绑定起来.
     Routing Key:路由关键字,exchange根据这个关键字进行消息投递。
     vhost:虚拟主机,一个broker里可以有多个vhost，用作不同用户的权限分离。
     Producer:消息生产者,就是投递消息的程序.
     Consumer:消息消费者,就是接受消息的程序.
     Channel:消息通道,在客户端的每个连接里,可建立多个channel.
     */

    @Autowired
    private AmqpTemplate amqpTemplate;

    public void sendHello(){
        String message = "hello "+new Date();
        System.out.println("RabbitSender.sendHello "+message);
        amqpTemplate.convertAndSend(RabbitMqConstant.QUEUE_HELLO, message);
    }

    public void sendObj(){
        Address address = new Address();
        address.setAddressId(1L);
        address.setProvince("广东省");
        address.setPhone("11111111111");
        address.setDefaultFlag(false);
        address.setCreateTime(new Date());
        address.setUpdateTime(new Date());
        address.setNow(LocalDateTime.now());

        amqpTemplate.convertAndSend(RabbitMqConstant.QUEUE_OBJECT, address);
    }

    public void sendMany1(int i){
        String message = "sendMany1 "+i;
        System.out.println("RabbitSender.sendMany1 "+message);
        amqpTemplate.convertAndSend(RabbitMqConstant.QUEUE_MANY, message);
    }

    public void sendMany2(int i){
        String message = "sendMany2 "+i;
        System.out.println("RabbitSender.sendMany2 "+message);
        amqpTemplate.convertAndSend(RabbitMqConstant.QUEUE_MANY, message);
    }

    public void sendFanout(){
        String message = "hello world";
        log.info("发送Fanout消息:{}", message);
        amqpTemplate.convertAndSend(RabbitMqConstant.EXCHANGE_FANOUT, "", message);
    }

    public void sendTopic1(){
        String context = "全体消息测试";
        amqpTemplate.convertAndSend(RabbitMqConstant.EXCHANGE_TOPIC, RabbitMqConstant.QUEUE_TOPIC_ANY, context);
    }

    public void sendTopic2(){
        String context = "消息1";
        amqpTemplate.convertAndSend(RabbitMqConstant.EXCHANGE_TOPIC, RabbitMqConstant.QUEUE_TOPIC_MESSAGE, context);
    }

    public void sendTopic3(){
        String context = "消息2";
        amqpTemplate.convertAndSend(RabbitMqConstant.EXCHANGE_TOPIC, RabbitMqConstant.QUEUE_TOPIC_MESSAGES, context);
    }
}
